include("../../static/js/config.js");
include("../../static/js/common.js");


$(function () {
    var table;
    var token = localStorage.getItem("token");

    var options = {
        url: config.server + '/admin/authority/list',
        method: 'POST',
        headers: {Authorization: 'Bearer ' + token},
        elem: '#table',
        page: true,
        curr: 1,
        count: 1,
        limit: 10,
        limits: [10, 20, 30, 40, 50, 60, 70, 80, 90],
        cols:[[
            {type:'checkbox'},
            {field:'name', title:'标识'},
            {field:'url', title:'地址'},
            {field:'createDate', title:'创建日期'},
            {fixed:'right', title:'操作', toolbar: '#toolbar'}
        ]],
        request: {
            pageName: 'current',
            limitName: 'size'
        },
        parseData: function (res) {
            reqAfter(res, function (data) {});
            return {
                "code": res.code === 200 ? 0 : res.code,
                "msg": res.msg,
                "count": res.data.total,
                "data": res.data.records
            };
        }
    };

    layui.use(['table'], function () {
        table = layui.table;
        table.render(options);

        table.on('tool(optEvent)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'edit':
                    layer.open({
                        area:['80%', '80%'],
                        type: 2,
                        content: config.url + '/page/authority/edit.html?id=' + data.id
                    });
                    break;
                case 'delete':
                    layer.confirm('确定删除吗?', function(delIndex) {
                        request('/admin/authority/delete', {id: data.id}, function (suc) {
                            if(suc.code == 200) {
                                obj.del();
                                layer.msg("删除成功", {icon: 1});
                            } else {
                                layer.msg("删除失败", {icon: 5});
                            }
                        });
                        layer.close(delIndex);
                    });
                    break;
            }
        });
    });

    $("#addBtn").click(function () {
        layer.open({
            area:['80%', '80%'],
            type: 2,
            content: config.url + '/page/authority/add.html'
        });
    });

    $("#deleteBatchBtn").click(function () {
        var checkStatus = table.checkStatus('table');
        var ids = [];
        for(var i = 0; i < checkStatus.data.length; i++){
            ids.push(checkStatus.data[i].id);
        }

        layer.confirm('确定删除吗?', function(delIndex) {
            request('/admin/authority/deleteBatch', {ids: ids}, function (suc) {
                if(suc.code == 200) {
                    layer.close(delIndex);
                    layer.msg("删除成功", {icon: 1});
                    setTimeout(function () {
                        window.location.reload();
                    }, 3000);
                } else {
                    layer.msg("删除失败", {icon: 5});
                }
            });

            layer.close(delIndex);
        });
    });

});